/**
 * 
 */
package pt.mleiria.bo;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import pt.mleiria.dao.Login;
import pt.mleiria.room.LivingRoom;
import pt.mleiria.room.Room;

/**
 * @author NB13712
 * 
 */
@Stateless
public class LoginEJB {
	private static final Logger LOG = Logger.getLogger(LoginEJB.class);

	@PersistenceContext(unitName = "DomusService")
	private EntityManager em;

	public boolean isUserAuthenticated(final String username,
			final String password) {
		try {
			Query q = em
					.createQuery("select l from Login l where l.username = :username and l.password = :password");
			q.setParameter("username", username);
			q.setParameter("password", password);
			Login login = (Login) q.getSingleResult();
			LOG.info("[" + login.getUsername() + ";" + login.getPassword()
					+ "]");
			return true;
		} catch (NoResultException e) {
			LOG.info("O User:[" + username + "] nao existe");
			LOG.error(e.getMessage());
			return false;
		}

	}

}
